package com.frejoys.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.frejoys.common.aop.Authority;
import com.frejoys.common.aop.Authorization;
import com.frejoys.common.constant.CommonConstant;
import com.frejoys.common.dto.admin.SetAgentDto;
import com.frejoys.common.dto.admin.StoreIdAndNameDto;
import com.frejoys.common.dto.admin.StoreIdAndPageDto;
import com.frejoys.common.dto.admin.StoreIdAndPhoneDto;
import com.frejoys.common.dto.app.StoreIdDto;
import com.frejoys.common.dto.common.IdDto;
import com.frejoys.common.dto.common.PageDto;
import com.frejoys.common.response.Result;
import com.frejoys.common.vo.admin.AdminAgentStoreListVo;
import com.frejoys.common.vo.admin.AdminAgentStoreVo;
import com.frejoys.common.vo.admin.StoreAndeAgentAndUserListVo;
import com.frejoys.service.AgentStoreService;
import com.frejoys.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "代理服务商家", description = "代理服务商家")
@RestController
@RequiredArgsConstructor
@RequestMapping(CommonConstant.routePrefixAdmin)
@Authorization(Authority.Private)
public class AdminAgentStoreController {

    private final AgentStoreService agentStoreService;

    private final UserService userService;

    /**
     * 获取店铺列表
     */
    @Operation(summary = "获取店铺列表")
    @PostMapping("/agent/store/list")
    public Result<List<AdminAgentStoreListVo>> listAgentStore() {
        return Result.success(agentStoreService.listAgentStore());
    }

    /**
     * 获取店铺详情及代理人信息
     *
     * @param dto 店铺id
     */
    @Operation(summary = "获取店铺详情及代理人信息")
    @PostMapping("/agent/store/info")
    public Result<AdminAgentStoreVo> getStoreInfo(@RequestBody @Validated IdDto dto) {
        return Result.success(agentStoreService.getStoreInfo(dto.getId()));
    }

    @Operation(summary = "修改代理人")
    @PostMapping("/agent/store/setAgent")
    public Result setAgent(@RequestBody @Validated SetAgentDto dto) {
        //校验代理人
        userService.verifyAgentUserId(dto.getAgentUserId());
        return Result.success(agentStoreService.setAgent(dto));
    }



    @Operation(summary = "获取店铺信息及代理人信息及店铺拥有者信息")
    @PostMapping("/agent/store/storeAndeAgentAndUserListInfo")
    public Result<IPage<StoreAndeAgentAndUserListVo>> storeAndeAgentAndUserListInfo(@RequestBody @Validated StoreIdAndPhoneDto dto) {
        return Result.success(agentStoreService.storeAndeAgentAndUserListInfo(dto));
    }
}
